package net.pulseproject.commons.util;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

public final class StringUtil {
	
	private StringUtil() {
		// util classes should not be instantiated
	}
	
	public static String prefixWithSet(final String propertyName) {
		if(propertyName == null) { throw new IllegalArgumentException("propertyName == null"); }
		if(propertyName.isEmpty()) { throw new IllegalArgumentException("propertyName is empty"); }
		
		final String firstLetter = propertyName.substring(0, 1);
		final String firstLetterCapitalized = firstLetter.toUpperCase(Locale.ENGLISH);
		return "set" + firstLetterCapitalized + propertyName.substring(1);
	}

	/**
	 * Splits with respect to multiple occurrences of the entity separator.
	 */
	public static String[] split(final String subject, final String entitySeparator) {
		if(subject == null) { throw new IllegalArgumentException("subject == null"); }
		if(entitySeparator == null) { throw new IllegalArgumentException("entitySeparator == null"); }
		if(entitySeparator.isEmpty()) { throw new IllegalArgumentException("entitySeparator is empty"); }
		
		if(subject.isEmpty()) {
			return new String[0];
		}
		final List<String> tmpResult = new LinkedList<String>();
		
		String leftOver = subject;
		while(leftOver.indexOf(entitySeparator) != -1) {
			final int nextIndex = leftOver.indexOf(entitySeparator);
			final String newToken = leftOver.substring(0, nextIndex);
			tmpResult.add(newToken);
			
			leftOver = leftOver.substring(newToken.length() + 1);
		}
		tmpResult.add(leftOver);
		
		return tmpResult.toArray(new String[tmpResult.size()]);
	}
	
	public static String[] trimElements(final String[] elements) {
		if(elements == null) { throw new IllegalArgumentException("elements == null"); }
		
		final String[] trimmed = new String[elements.length];
		for (int i = 0; i < elements.length; i++) {
			trimmed[i] = elements[i].trim();
		}
		return trimmed;
	}
	
	public static String[] splitAndTrimElements(final String subject, final String entitySeparator) {
		final String[] splitted = StringUtil.split(subject, entitySeparator);
		return StringUtil.trimElements(splitted);
	}
	
	
	public static String exceptionToString(final Exception exception) {
		if(exception == null) { throw new IllegalArgumentException("exception == null"); }
		final StringWriter writer = new StringWriter();
		final PrintWriter pWriter = new PrintWriter(writer);
		exception.printStackTrace(pWriter);
		return writer.toString();
	}

	public static boolean matchesRegExps(final String subject, final String[] portFilterRegExps) {
		for (final String regExp : portFilterRegExps) {
			final boolean matches = subject.matches(regExp);
			if(matches == true) {
				return true;
			}
		}
		return false;
	}

	public static int[] splitTwoDigitsBySeparator(final String subject, final String separator) {
		final String[] parts = StringUtil.split(subject, separator);
		if(parts.length != 2) {
			throw new IllegalArgumentException("parts.length [" + parts.length + "] != 2");
		}
		final int x = (int) Double.parseDouble(parts[0]);
		final int y = (int) Double.parseDouble(parts[1]);
		return new int[] { x, y };
	}

}
